<template>
  <div>
    <el-form labelWidth="120px" :disabled="status==1">
      <!-- {{summoney}} -->
      <getreimlen :lastreimarr.sync="lastreimarr" :status="reimstatus"></getreimlen>

      <el-form-item label="报销内容" class="is-required" style="width:100%;">
        <el-input v-model="reimData.content" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.reimData.content.required">
          请输入报销内容
        </div>
      </el-form-item>
      <el-form-item label="报销时间" class="is-required" style="width:100%;">
        <el-date-picker
          v-model="reimData.examinetime"
          type="date"
          style="width:100%;"
          placeholder="选择申请时间"
          valueFormat="yyyy-MM-dd"
          clearable
        ></el-date-picker>
        <div class="el-form-item__error" v-if="!$v.reimData.examinetime.required">
          请选择报销时间
        </div>
      </el-form-item>
      <el-form-item label="支付方式" class="is-required" style="width:100%;">
        <el-select v-model="reimData.paytype" placeholder="请选择支付方式" style="width:100%;">
          <el-option
            v-for="item in reimtypearr"
            :key="item.value"
            :label="item.hash.code_name"
            :value="item.hash.code_id"
          ></el-option>
        </el-select>
        <div class="el-form-item__error" v-if="!$v.reimData.paytype.required">
          请选择支付方式
        </div>
      </el-form-item>
      <el-form-item label="关联审批" style="width:100%;">
        <el-select v-model="reimData.capitalid" placeholder="请选择关联审批" style="width:100%;" clearable>
          <el-option
            v-for="item in moneydata"
            :key="item.value"
            :label="item.hash.deptname+'-'+item.hash.capitaltype+'-'+item.hash.amount+'('+item.hash.intime+')'"
            :value="item.hash.capitalid"
          ></el-option>
        </el-select>
        <!-- <div class="el-form-item__error" v-if="!$v.reimData.capitalid.required">
          请选择请款类型
        </div> -->
      </el-form-item>
      <el-form-item label="总金额(元)" style="width:100%;">
        {{summoney}}
      </el-form-item>
      <el-form-item label="大写金额" style="width:100%;">{{this.convertCurrency(summoney)}}</el-form-item>

      <el-form-item label="附件数量" style="width:100%;">
        <el-input v-model="reimData.filenumber" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注" style="width:100%;">
        <el-input v-model="reimData.beizhu" clearable></el-input>
      </el-form-item>
      <el-form-item label="报销附件" style="width:100%;">
        <upload2 :filelimit="5" :filelink.sync="filearr1" :ispic="false" :isedit="false"></upload2>
      </el-form-item>
      <el-form-item label="选择审批流" v-if="status!=1" class="is-required" style="width:100%;">
        <choosespl @getsplid="getsplid" :atypeid="'0101'" :num="summoney" v-if="ischoosespl"></choosespl>
        <div class="el-form-item__error" v-if="!$v.aid.required">
          请选择审批流
        </div>
      </el-form-item>
    </el-form>
    <div style="text-align:right" v-if="status==0">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitreim">确 定</el-button>
    </div>
    <div style="text-align:right" v-if="status==2">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitreim2">重新提交</el-button>
    </div>
  </div>
</template>
<script>
import choosespl from '~/components/apply/choosespl.vue';
import upload2 from '~/components/common/upload2.vue';
import getreimlen from '~/components/common/getreimlen.vue';
import { required } from 'vuelidate/lib/validators';

export default {
  validations: {
    aid: {
      required,
    },
    reimData: {
      content: {
        required,
      },
      examinetime: {
        required,
      },
      paytype: {
        required,
      },
    },
  },
  data() {
    return {
      flag: false,
      ischoosespl: false,
      dialogVisible: true,
      ts: this.newStore(),
      filearr1: [],
      moneydata: [],
      reimstatus: '',
      reimtypearr: [],
      // mounttyper: [],
      lastreimarr: [],
      //   photoarr: [],
      reimData: {
        mounttype: [],
        branchid: [],
        amount: [],
        paytype: '',

      },
      aid: '',
    };
  },
  watch: {
    summoney() {
      this.ischoosespl = false;
      this.$nextTick(() => {
        this.ischoosespl = true;
      });
    },
  },
  computed: {
    summoney() {
      let a = 0;
      console.log(this.lastreimarr, 'sum');
      for (let i = 0; i < this.lastreimarr.length; i++) {
        a += Number(this.lastreimarr[i].amount);
      }
      a.toFixed(2)
      return a;
    },
  },
  created() {
    this.getmoney();
    console.log(this.status);
    this.reimstatus = this.status;
    this.lastreimarr = [];
    this.getcodeData('0501', '05', 'reimtypearr');
    if (this.status != 0) {
      for (let i = 0; i < this.checkdetail.operation3.length; i += 1) {
        const a = {};
        a.url = this.checkdetail.operation3[i].hash.fileurl;
        a.name = this.checkdetail.operation3[i].hash.filename;
        this.filearr1.push(a);
      }
      for (let i = 0; i < this.checkdetail.operation2.length; i += 1) {
        const b = {};
        b.mounttype = this.checkdetail.operation2[i].hash.mounttype;
        b.childmounttype = this.checkdetail.operation2[i].hash.childmounttype;
        b.branchid = this.checkdetail.operation2[i].hash.branchid;
        b.amount = this.checkdetail.operation2[i].hash.amount;
        this.lastreimarr.push(b);
      }
      this.reimData = this.checkdetail.operation1[0].hash;
      // this.summoney = this.reimData.amountsum;
      this.reimData.capitalid =
        `${this.checkdetail.operation1[0].hash.capitaltype
        }-${
          this.checkdetail.operation1[0].hash.amount}`;


      // this.reimData.mounttype = [];
      // this.reimData.branchid = [];
    }
  },
  components: {
    choosespl,
    upload2,
    getreimlen,
  },
  props: {
    status: Number,
    checkdetail: Object,
    fun: Function,
  },
  methods: {
    closedia() {
      this.$emit('closedia', false);
      this.$router.go(-1);
    },
    checksplnum(money, splid) {
      const req = {
        handle: '9000B8B8D347526960C5BAB1FB6894EF3277431D02D636A5015C1425899A1F59A41CF7A197311E4A',
        flowid: splid,
        number: money,
      };
      const p = this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.data.operation1[0].hash.cou == '0') {
          this.flag = false;
        } else {
          this.flag = true;
        }
      });
      return p;
    },
    submitreim2() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.summoney, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认重新提交报销审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req2 = this.reimData;
              this.reimData.mounttype = [];
              this.reimData.childmounttype = [];
              this.reimData.branchid = [];
              this.reimData.amount = [];
              if (this.lastreimarr.length == 0) {
                this.$message({
                  type: 'error',
                  message: '请选择报销种类！',
                });
                return;
              }
              for (let i = 0; i < this.lastreimarr.length; i += 1) {
                if (this.lastreimarr[i].mounttype == '' || this.lastreimarr[i].mounttype == null) {
                  this.$message({
                    type: 'error',
                    message: '请选择报销种类！',
                  });
                  return;
                }
                if (this.lastreimarr[i].childmounttype == '' || this.lastreimarr[i].childmounttype == null) {
                  this.$message({
                    type: 'error',
                    message: '请选择报销子类！',
                  });
                  return;
                }
                if (this.lastreimarr[i].amount == '' || this.lastreimarr[i].amount == null) {
                  this.$message({
                    type: 'error',
                    message: '请选择结报销金额！',
                  });
                  return;
                }
                if (this.lastreimarr[i].branchid == '' || this.lastreimarr[i].branchid == null) {
                  this.$message({
                    type: 'error',
                    message: '请选择部门！',
                  });
                  return;
                }
                console.log(this.lastreimarr[i], this.reimData, 'this.lastreimarr[i].amount');

                this.reimData.amount.push(this.lastreimarr[i].amount);
                this.reimData.mounttype.push(this.lastreimarr[i].mounttype);
                this.reimData.childmounttype.push(this.lastreimarr[i].childmounttype);
                this.reimData.branchid.push(this.lastreimarr[i].branchid);
              }

              const req = {
                handle:
              '9000B8B8D347526960C5BAB1FB6894EF17427C100F77D58DB382894F762E66D209652941AB86E5AD',
                atype: '0101',
                flowid: this.aid,
                approvalid: this.reimData.aid,
              };
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle = '0398B14C0EC6F01867D31307B7D9BCF9FAD73AA16B44841DDEC71ADD7F717FFB';

                req2.bigamountsum = this.convertCurrency(this.summoney);
                req2.amountsum = this.summoney;
                req2.fileurl = this.posturl(this.filearr1);
                req2.filename = this.postname(this.filearr1);
                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '重新提交成功！',
                  });
                  this.fun();
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    submitreim() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.summoney, this.aid).then(() => {
          console.log(this.flag, 'flag');

          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认提交报销审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle:
              '9000B8B8D347526960C5BAB1FB6894EF8B7B312373E9BEF47A8517CE4098C19A',
                atype: '0101',
                flowid: this.aid,
              };
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.reimData;
                this.reimData.mounttype = [];
                this.reimData.childmounttype = [];
                this.reimData.branchid = [];
                if (this.lastreimarr.length == 0) {
                  this.$message({
                    type: 'success',
                    message: '请选择报销种类！',
                  });
                  return;
                }
                for (let i = 0; i < this.lastreimarr.length; i += 1) {
                  if (this.lastreimarr[i].mounttype == '' || this.lastreimarr[i].mounttype == null) {
                    this.$message({
                      type: 'success',
                      message: '请选择报销种类！',
                    });
                    return;
                  }
                  if (this.lastreimarr[i].childmounttype == '' || this.lastreimarr[i].childmounttype == null) {
                    this.$message({
                      type: 'success',
                      message: '请选择报销子类！',
                    });
                    return;
                  }
                  if (this.lastreimarr[i].amount == '' || this.lastreimarr[i].amount == null) {
                    this.$message({
                      type: 'success',
                      message: '请选择结报销金额！',
                    });
                    return;
                  }
                  if (this.lastreimarr[i].branchid == '' || this.lastreimarr[i].branchid == null) {
                    this.$message({
                      type: 'success',
                      message: '请选择部门！',
                    });
                    return;
                  }
                  this.reimData.amount.push(this.lastreimarr[i].amount);
                  this.reimData.mounttype.push(this.lastreimarr[i].mounttype);
                  this.reimData.childmounttype.push(this.lastreimarr[i].childmounttype);
                  this.reimData.branchid.push(this.lastreimarr[i].branchid);
                }

                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle =
              '0398B14C0EC6F01867D31307B7D9BCF939F07FD7772BE620141971E42273D554';
                req2.bigamountsum = this.convertCurrency(this.summoney);
                req2.amountsum = this.summoney;
                req2.fileurl = this.posturl(this.filearr1);
                req2.filename = this.postname(this.filearr1);
                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '发起报销审批成功！',
                  });
                  this.$emit('closedia', false);
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },

    getcodeData(supid, codetype, arrname) {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid,
        codetype,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this[arrname] = ts.d.data.operation1;
        // console.log(ts.d.data.operation1,'支付方式')
      });
    },
    // 获取请款类型
    getmoney() {
      const req = {
        handle:
          '0398B14C0EC6F01867D31307B7D9BCF926057EC38BCF4C1D8624F1C86CD24E67',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.moneydata = ts.d.data.operation1;
        console.log(this.moneydata);
      });
    },
    // 公共方法图片数组转路径数组
    posturl(arr) {
      const a = [];
      for (let i = 0; i < arr.length; i += 1) {
        a.push(arr[i].url);
      }
      return a;
    },
    // 公共方法图片数组转名称数组
    postname(arr) {
      const b = [];
      for (let i = 0; i < arr.length; i += 1) {
        b.push(arr[i].name);
      }
      return b;
    },
    getsplid(val) {
      this.aid = val;
    },

    // 金额转大写
    convertCurrency(money) {
      // 汉字的数字
      const cnNums = new Array(
        '零',
        '壹',
        '贰',
        '叁',
        '肆',
        '伍',
        '陆',
        '柒',
        '捌',
        '玖',
      );
      // 基本单位
      const cnIntRadice = new Array('', '拾', '佰', '仟');
      // 对应整数部分扩展单位
      const cnIntUnits = new Array('', '万', '亿', '兆');
      // 对应小数部分单位
      const cnDecUnits = new Array('角', '分', '毫', '厘');
      // 整数金额时后面跟的字符
      const cnInteger = '整';
      // 整型完以后的单位
      const cnIntLast = '元';
      // 最大处理的数字
      const maxNum = 999999999999999.9999;
      // 金额整数部分
      let integerNum;
      // 金额小数部分
      let decimalNum;
      // 输出的中文金额字符串
      let chineseStr = '';
      // 分离金额后用的数组，预定义
      let parts;
      if (money == '') {
        return '';
      }
      money = parseFloat(money);
      if (money >= maxNum) {
        // 超出最大处理数字
        return '';
      }
      if (money == 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
        return chineseStr;
      }
      // 转换为字符串
      money = money.toString();
      if (money.indexOf('.') == -1) {
        integerNum = money;
        decimalNum = '';
      } else {
        parts = money.split('.');
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
      }
      // 获取整型部分转换
      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0;
        const IntLen = integerNum.length;
        for (var i = 0; i < IntLen; i++) {
          var n = integerNum.substr(i, 1);
          const p = IntLen - i - 1;
          const q = p / 4;
          const m = p % 4;
          if (n == '0') {
            zeroCount++;
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0];
            }
            // 归零
            zeroCount = 0;
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m == 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q];
          }
        }
        chineseStr += cnIntLast;
      }
      // 小数部分
      if (decimalNum != '') {
        const decLen = decimalNum.length;
        for (var i = 0; i < decLen; i++) {
          var n = decimalNum.substr(i, 1);
          if (n != '0') {
            chineseStr += cnNums[Number(n)] + cnDecUnits[i];
          }
        }
      }
      if (chineseStr == '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
      } else if (decimalNum == '') {
        chineseStr += cnInteger;
      }
      return chineseStr;
    },
  },
};
</script>
<style scoped>
/* .deleteicon{
    font-size: 20px;
    text-align: center;
    height: 40px;
    line-height: 56px;
    margin-left: 130px;
    color: #aaa;
    cursor: pointer;
}
.deleteicon:hover{
    color: coral;
} */
</style>
